package com.wave.wavesystem.business.controller;

import com.mybatisflex.core.paginate.Page;
import com.wave.wavesystem.business.pojo.dto.role.RoleQueryRequest;
import com.wave.wavesystem.business.pojo.entity.RoleEntity;
import com.wave.wavesystem.business.pojo.vo.RoleVO;
import com.wave.wavesystem.business.service.RoleService;
import wave.common.pojo.enums.RStatusEnum;
 import wave.common.pojo.entity.Result;
import com.wave.wavesystem.common.util.Condition;
import com.wave.wavesystem.common.util.Func;

import javax.annotation.Resource;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;

import java.util.List;

/**
 * @author 夕下风波
 * @date 2025-02-11
 * @description 角色表 访问层
 */
@RestController
@RequestMapping("/role")
@Tag(name = "角色表")
public class RoleController {

    @Resource
    private RoleService roleService;

    /**
     * 新增
     *
     * @param role 新增实体
     */
    @Operation(summary = "保存")
    @PostMapping("/save")
    public Result<RoleEntity> save(@RequestBody RoleEntity role) {
        roleService.save(role);
        return Result.data(role);
    }

    /**
     * 删除/批量删除
     *
     * @param ids id集合
     */
    @Operation(summary = "删除")
    @PostMapping("/remove")
    public Result<Boolean> remove(String ids) {
        List<String> idList = Func.strToStringList(ids);
        return Result.status(roleService.removeByIds(idList), RStatusEnum.SUCCESS);
    }

    /**
     * 更新
     *
     * @param role 更新内容
     */
    @Operation(summary = "更新")
    @PostMapping("/update")
    public Result<RoleEntity> update(@RequestBody RoleEntity role) {
        roleService.updateById(role);
        return Result.data(roleService.getById(role.getId()), RStatusEnum.SUCCESS);
    }

    /**
     * 详情查询
     *
     * @param roleQueryRequest 查询条件
     */
    @Operation(summary = "详情")
    @GetMapping("/detail")
    public Result<RoleEntity> detail(RoleQueryRequest roleQueryRequest) {
        return Result.data(roleService.getById(roleQueryRequest.getId()));
    }

    /**
     * 列表查询
     *
     * @param roleQueryRequest 查询条件
     */
    @Operation(summary = "列表")
    @GetMapping("/list")
    public Result<List<RoleEntity>> list(RoleQueryRequest roleQueryRequest) {
        RoleEntity role = new RoleEntity();
        Func.beanCopy(roleQueryRequest, role);
        return Result.data(roleService.list(Condition.getQueryWrapper(role)));
    }

    /**
     * 条件分页查询
     *
     * @param roleQueryRequest  查询条件
     */
    @Operation(summary = "分页")
    @GetMapping("/page")
    public Result<Page<RoleEntity>> page(RoleQueryRequest roleQueryRequest) {
        RoleEntity role = new RoleEntity();
        Func.beanCopy(roleQueryRequest, role);
        return Result.data(roleService.page(new Page<>(roleQueryRequest.getPageNum(), roleQueryRequest.getPageSize()),
                Condition.getQueryWrapper(role)));
    }
    /**
     * 获取所有角色
     */
    @Operation(summary = "获取所有角色")
    @GetMapping("/allRole")
    public Result<List<RoleVO>> allRole() {
        return Result.data(roleService.allRole());
    }

}